﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmTimKiemDonDatHang : Form
    {
        public frmTimKiemDonDatHang()
        {
            InitializeComponent();
        }
        private string hanhDong;

        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private string maKho;
        public string MaKho
        {
            get { return maKho; }
            set { maKho = value; }
        }
        private string maNhaCungCap;

        public string MaNhaCungCap
        {
            get { return maNhaCungCap; }
            set { maNhaCungCap = value; }
        }
        public frmTimKiemDonDatHang(string hanhDong, string maKho, string maNhaCungCap)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.maKho = maKho;
            this.maNhaCungCap = maNhaCungCap;
        }
        public frmTimKiemDonDatHang(string hanhDong)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
        }
        private void frmTimKiemDonDatHang_Load(object sender, EventArgs e)
        {
            try
            {
                switch (hanhDong)
                {
                    case "NhapKho":
                        HienThi();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private Entities.DonDatHangNCC[] dondathang;
        private Entities.DonDatHangNCC banghi;

        #region Hien Thi
        private void HienThi()
        {
            try
            {
                Entities.DonDatHangNCC ncc = new Entities.DonDatHangNCC();
                ncc.HanhDong = "5";
                ncc.MaKho = this.maKho;
                ncc.MaDonDatHangNCC ="";
                ncc.MaNhaCungCap = this.maNhaCungCap;
                this.dondathang = Common.StaticVariable.InterfaceObject.SelectDonDatHangNCC(ncc);
                dgvHienThi.DataSource = dondathang;
                dgvHienThi.Rows[0].Selected = true;
                Gridview();
            }
            catch (Exception ex)
            {
                dondathang = null;
                banghi = null;
                Gridview();
                string s = ex.Message;
            }
        }

        private void Gridview()
        {
            try
            {
                if (dondathang == null)
                {
                    Entities.DonDatHangNCC[] nv = new Entities.DonDatHangNCC[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaDonDatHangNCC"].Visible = true;
                dgvHienThi.Columns["MaDonDatHangNCC"].HeaderText = "Mã đơn đặt hàng";
                dgvHienThi.Columns["NgayDonHang"].Visible = true;
                dgvHienThi.Columns["NgayDonHang"].HeaderText = "Ngày đặt hàng";
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        private string MaDonDatHangNCC = string.Empty;
        private void GetRow()
        {
            try
            {
                if (dondathang == null) { return; }
                for (int i = 0; i < this.dondathang.Length; i++)
                {
                    if (dondathang[i].MaDonDatHangNCC == this.MaDonDatHangNCC)
                    {
                        banghi = dondathang[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        Common.StaticVariable.dathangnhacungcap = null;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion
        private void Chon()
        {
            try
            {
                if (this.banghi == null)
                {
                    MessageBox.Show("Bạn phải chọn đơn đặt hàng.", "Thông báo");
                    return;
                }
                Common.StaticVariable.dathangnhacungcap = banghi;
                this.Close();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            MaDonDatHangNCC = dgvHienThi.CurrentRow.Cells["MaDonDatHangNCC"].Value.ToString();
            this.GetRow();
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Chon();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Chon();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }
    }
}
#endregion
